
import re, sys
ID = re.compile(r'^[a-zA-Z_]\w*$')

if len(sys.argv) < 4:
    print "Too few arguments"
    sys.exit(1)

flag = sys.argv[1]


f = open(sys.argv[2])
standard_names = []
for name in f.readlines():
    if name[0] != '#':
        n = name.strip()
        standard_names.append(n)
f.close()
if len(standard_names) != 256:
    print ("Should be 256 standard names (found %d)" %  len(standard_names))
    sys.exit(1)

if flag == '-j':
    
    java = open(sys.argv[3], 'w')
    print >> java, 'package parser;\nclass StandardNames {'
    index = 0
    for n in standard_names:
        if ID.match(n) and n not in ('int', 'float', 'long', 'super'):
            print >> java, '    static final int %s = %d;' % (n, index)
        index += 1
    
    print >> java, '    static final String[] names = {'
    for n in standard_names:
        print >> java, '        "%s",' % n
    print >> java, '    };'
    print >> java, '}'
    java.close()

elif flag == '-h':
    
    H = open(sys.argv[3], 'w')
    index = 0
    for n in standard_names:
        if ID.match(n) and n != 'int' and n != 'float' and n != 'long':
            sn = 'standard_name'
            print >> H, '#define %s_index_%s %d' % (sn, n, index)
            print >> H, '#define %s_%s %s[%d]' % (sn, n, sn, index)
        index += 1
    H.close()


else:
    print "Unexpected flag:", flag
    sys.exit(1)

#This belongs elsewhere:
f = open('version.txt', 'r')
num = int(f.readline())
f.close()

java = open('parser/Version.java', 'w')
print >> java, 'package parser;\nclass Version {'
print >> java, "static final int NUMBER = %d; " % num
print >> java, '}'

java.close()
    

    
